﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace cninfo
{
    class MySpider
    {
        public string Domain = "http://59.40.185.96";
        public string WebSite = "http://59.40.185.96/search/search.jsp";
        public string StartDate = "2011-05-30";
        public string EndDate = "2011-05-30";
        public int PageNo = 1;

        public List<Notice> CollectNotice()
        {
            List<Notice> result = new List<Notice>();
            List<Notice> lastlist = new List<Notice>();

            bool equal = false;
            this.PageNo = 0;
            while (!equal)
            {
                this.PageNo++;
                //Console.WriteLine("PageNo={0}", this.PageNo.ToString());
                string content = this.SendPostRequest();
                List<Notice> list = this.ParseWebPage(content, this.Domain);
                //Console.WriteLine(list.Count);
                if (list.Count == lastlist.Count)
                {
                    bool e = true;
                    for (int i = 0; i < lastlist.Count; i++)
                    {
                        if (!lastlist[i].Equals(list[i]) )
                        {
                            e = false;
                            break;
                        }
                    }
                    equal = e;
                }
                if (!equal)
                {
                    lastlist = list;
                    result.AddRange(list);
                }
            }

            return result;
        }

        public string SendPostRequest()
        {
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            string postData = "orderby=code01";   //code11, date01, date11
            postData += ("&marketType=");
            postData += ("&noticeType=");
            postData += ("&stockCode=");
            postData += ("&keyword=");
            postData += string.Format("&startTime={0}", this.StartDate);
            postData += string.Format("&endTime={0}", this.EndDate);
            postData += string.Format("&pageNo={0}", this.PageNo.ToString());


            byte[] data = myEncoding.GetBytes(postData);

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.WebSite);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            myRequest.ContentLength = data.Length;

            using (Stream newStream = myRequest.GetRequestStream())
            {
                newStream.Write(data, 0, data.Length);
            }

            string content = "";
            using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), myEncoding);
                content = reader.ReadToEnd();
                //Console.WriteLine(content);
            }

            return content;
        }


        public List<Notice> ParseWebPage(string content, string domain)
        {
            List<Notice> result = new List<Notice>();

            string startTag = "<div class=\"fg\">";
            string endTag = "<div class=\"fy\">";
            string flagTag = "<li><span class=\"zz\">";
            string notflagTag = "没有找到你搜索的公告";
            int start = content.IndexOf(startTag);
            int end = content.IndexOf(endTag);
            if (start < 0 || end < 0 || start >= end)      
            {
                //报错
                throw new Exception("页面格式找不到开头或结尾标记");
                return result;
            }

            content = content.Substring(start, end - start);
            //Console.WriteLine(content);
            string[] lines = content.Split(new string[] { "</li>" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines)
            {
                //Console.WriteLine(line);
                if ( (line.IndexOf(flagTag) >= 0)  && (line.IndexOf(notflagTag) < 0) )
                {
                    Notice n = new Notice();
                    n.ParseFromLine(line, domain);
                    result.Add(n);
                }
            }

            return result;
        }
    }
}
